﻿using System.Text;

namespace WPHttpServer
{
    public class TextResponseContentProvider : IResponseContentProvider
    {
        public string TextContent { get; set; }
        public CharsetInfo Charset { get; set; }
        public virtual string MimeType { get; protected set; }

        public TextResponseContentProvider(string textContent)
            : this(textContent, GetUtf8CharsetInfo())
        {            
        }

        public TextResponseContentProvider(string textContent, CharsetInfo charset)
        {
            TextContent = textContent;
            Charset = charset;

            this.MimeType = "text/plain";
        }

        private static CharsetInfo GetUtf8CharsetInfo()
        {
            return new CharsetInfo(Encoding.UTF8, "utf-8");
        }

        public byte[] GetContentData()
        {
            return Encoding.UTF8.GetBytes(this.TextContent);
        }

        public string ContentMediaTypeName
        {
            get
            {
                return string.Format("{0}; charset={1}", MimeType, Charset.Name);
            }
        }
    }
}
